package com.gcd.skymallorderitemapi.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcd.skymallorderitemapi.dao.OrderItemMapper;
import com.gcd.skymallorderitemapi.service.ProductFeignService;
import com.situ.model.Cart;
import com.situ.model.OrderItem;
import com.situ.model.Product;
import com.situ.service.OrderItemService;
import com.situ.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OrderItemServiceImpl implements OrderItemService {
    private OrderItemMapper orderItemMapper;
    private ProductFeignService productFeignService;

    @Autowired
    public void setOrderItemMapper(OrderItemMapper orderItemMapper) {
        this.orderItemMapper = orderItemMapper;
    }

    @Autowired
    public void setProductFeignService(ProductFeignService productFeignService) {
        this.productFeignService = productFeignService;
    }

    //查询所有
    @Override
    public Page<OrderItem> findAll(Page<OrderItem> page, OrderItem orderItem) {
        return orderItemMapper.findAll(page, orderItem);
    }

    //根据订单id查询
    @Override
    public Page<OrderItem> findByOrderId(Page<OrderItem> page, Integer orderId) {
        return orderItemMapper.findByOrderId(page, orderId);
    }

    //删除订单项
    @Override
    public Integer deleteOrderItem(List<Integer> ids) {
        return orderItemMapper.deleteByIds(ids);
    }

    //修改订单项
    @Override
    public boolean updateOrderItem(OrderItem orderItem) {
        return orderItemMapper.updateById(orderItem) == 1;
    }

    //创建普通订单项
    //todo添加事务
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createOrderItem(List<Cart> carts) {
        for (Cart cart : carts) {
            ResponseEntity<JsonResult<?>> result = productFeignService.getById(cart.getProductId());
            JsonResult<?> jsonResult = result.getBody();
            if (jsonResult != null && jsonResult.getData() != null) {
                Product product = (Product) jsonResult.getData();

                //创建订单项
                OrderItem orderItem = new OrderItem();
                orderItem.setUserId(cart.getUserId());
                orderItem.setOrderType(0);
                orderItem.setProductId(cart.getProductId());
                orderItem.setProductName(product.getName());
                orderItem.setProductPrice(product.getPrice());
                orderItem.setQuantity(cart.getQuantity());
                orderItem.setOrderType(0);
                orderItem.setUserId(cart.getUserId());
                BigDecimal itemTotal = product.getPrice().multiply(new BigDecimal(cart.getQuantity()));
                orderItem.setTotalPrice(itemTotal);
                //插入到数据库
                int flag = orderItemMapper.insert(orderItem);
                if (flag != 1) {
                    return false;
                }
            } else {
                // 商品信息获取失败，创建订单项失败
                return false;
            }
        }
        return true;
    }

    //创建秒杀订单项
    @Override
    public boolean createSeckillOrderItem(OrderItem orderItem) {
        return orderItemMapper.insert(orderItem)==1;
    }
}
